package com.four.bergain.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.four.bergain.dao.BargainMapper;
import com.four.bergain.dao.BargainPropertyMapper;
import com.four.bergain.dao.BargainPeopleMapper;
import com.four.bergain.dao.BargainUserMapper;
import com.four.bergain.entity.Bargain;
import com.four.bergain.entity.BargainPeople;
import com.four.bergain.entity.BargainProperty;
import com.four.bergain.entity.BargainUser;
import com.four.bergain.feign.UserFeginService;
import com.four.bergain.service.IBargainPeopleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.four.bergain.util.RandomPriceUtils;
import com.guli.common.utils.R;
import com.guli.common.vo.UserVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gs
 * @since 2021-12-19
 */
@Service
public class BargainPeopleServiceImpl extends ServiceImpl<BargainPeopleMapper, BargainPeople> implements IBargainPeopleService {

    @Autowired
    private BargainMapper bargainMapper;
    @Autowired
    private BargainPropertyMapper propertyMapper;
    @Autowired
    private BargainPeopleMapper peopleMapper;
    @Autowired
    private BargainUserMapper userMapper;
    @Autowired
    private UserFeginService feginService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R savePeople(Integer uid, Integer baId, Integer userId) {
        //查看该用户信息
        UserVo vo = feginService.getUserById(userId);
        if (vo==null) {
            return R.error(500,"该用户不存在");
        }
        //查询被帮忙用户是否砍过此商品
        BargainUser user = userMapper.getById(uid,baId);
        int a = 2;
        if (user==null || user.getBarStatus()==a) {
            return R.error(500,"被帮忙用户没有砍过或者已经过期");
        }
        //查询该帮忙用户是否砍过 一个人只能帮砍一次
        BargainPeople property = peopleMapper.getBargainById(userId,user.getBarUid());
        if (property!=null) {
            return R.error(500,"您已经帮该用户砍过价了");
        }
        //添加帮助拼团表
        BargainPeople people = new BargainPeople();
        people.setBerUid(user.getBarUid());
        people.setUserId(vo.getUserId());
        people.setUserName(vo.getUserName());
        people.setBergainCreateTime(new Date());
        //获取砍价砍了多少
        String s = redisTemplate.opsForValue().get("four:bergain:bargainuser:randomprice");
        List<Double> doubles = JSON.parseObject(s,new TypeReference<List<Double>>(){});
        if (s == null) {
            BargainProperty byBaId = propertyMapper.getByBaId(user.getBarId());
            doubles = RandomPriceUtils.randomPrice(
                    user.getBarMuch(),
                    (int) (byBaId.getParaPrice().subtract(user.getBergainPrice()).doubleValue() * 100));
        }
        Double aDouble = doubles.get(user.getBarMuch() - 1);
        people.setBergainPrice(new BigDecimal(aDouble));
        peopleMapper.savePeople(people);

        //修改砍价次数
        user.setBarMuch(user.getBarMuch()-1);
        //总砍价
        user.setBarTotalCutting(user.getBarTotalCutting().add(new BigDecimal(aDouble)));
        userMapper.updateBargainUserByMuch(user.getBarMuch(),user.getBarTotalCutting(),user.getBarUid());


        return R.ok("您已经砍价成功");
    }
}
